package com.example.androidexample;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.example.basic.configinfo.APKInfo;
import com.example.basic.configinfo.DeviceInfo;
import com.example.basic.lifecycle.ApplicationStatus;
import com.example.basic.utils.ApplicationContextUtils;
import com.example.clib.NativeLibLoader;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

public class App extends Application {

    private static final String TAG = "App_Log";

    @Override
    public void onCreate() {
        super.onCreate();

        //在存在子进程的时候 这个地方会被回调多次
        if (isMainProcess()) {
            long mStartTime = System.currentTimeMillis();
            //初始化友盟
            ApplicationStatus.initialize(this);
            UMConfigure.preInit(getApplicationContext(),BuildConfig.UM_APPKEY,"oapm");
            UMConfigure.setLogEnabled(BuildConfig.LOG_DEBUG);
            MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
        /*
        deviceType：设备类型，UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子
         */
            UMConfigure.init(getApplicationContext(),BuildConfig.UM_APPKEY,"oapm",UMConfigure.DEVICE_TYPE_PHONE,null);

            APKInfo.initApkInfo(this,"oapm");
            DeviceInfo.init(this);
            //加载内核
            NativeLibLoader.init();
            Log.d(TAG, "onCreate: dTime = " + (System.currentTimeMillis() - mStartTime));
        }
    }


    /**
     * 判断是否是主进程
     *
     * @return 是否是主进程
     */
    private boolean isMainProcess() {
        try {
            String processName = "";
            int pid = android.os.Process.myPid();
            ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService
                    (Context.ACTIVITY_SERVICE);
            for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
                if (process.pid == pid) {
                    processName = process.processName;
                }
            }
            return getApplicationContext().getPackageName().equals(processName);
        } catch (Exception e) {
            // isolatedProcess 设置为true的进程会抛出异常
            e.printStackTrace();
            return false;
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        ApplicationContextUtils.initApplicationContext(this);
    }
}
